package com.ohos.carbon.provider;

import com.ohos.carbon.ResourceTable;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.element.ElementScatter;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

public abstract class RecyclerAdapter<VH extends RecyclerAdapter.ViewHolder> extends BaseItemProvider {

//    public static final int NO_POSITION = 0;

    public abstract ViewHolder onCreateViewHolder(ComponentContainer componentContainer);

    public abstract int getItemCount();

    public abstract void onBindViewHolder(VH holder, int position);

    private Context context;

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        ViewHolder itemViewHolder;
//        Text text = new Text(context);
//        text.setWidth(StackLayout.LayoutConfig.MATCH_PARENT);
//        text.setHeight(StackLayout.LayoutConfig.MATCH_CONTENT);
//        text.setTextSize(25);
//        if (position == 0){
//            text.setText("aaa");
//        }
        itemViewHolder = onCreateViewHolder(componentContainer);

        itemViewHolder.itemComponent.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                int action = touchEvent.getAction();
                switch (action) {
                    case TouchEvent.PRIMARY_POINT_DOWN:
                        itemViewHolder.itemComponent.setBackground(ElementScatter.getInstance(componentContainer.getContext()).parse(ResourceTable.Graphic_bg_color_green));
                        break;
                    case TouchEvent.CANCEL:
                    case TouchEvent.PRIMARY_POINT_UP:
                        itemViewHolder.itemComponent.setBackground(ElementScatter.getInstance(componentContainer.getContext()).parse(ResourceTable.Graphic_background_color_white));
                        break;
                }
                return true;
            }
        });
        onBindViewHolder((VH) itemViewHolder, position);
        return itemViewHolder.itemComponent;
    }

    @Override
    public int getCount() {
        return getItemCount();
    }

//    @Override
//    public void notifyDataSetItemRemoved(int position) {
//        super.notifyDataSetItemRemoved(position);
//    }

    public class ViewHolder {
        //        int mPosition = NO_POSITION;
//        int mPreLayoutPosition = NO_POSITION;
        Component itemComponent;

        public ViewHolder(Component itemComponent) {
            if (itemComponent == null) {
                throw new IllegalArgumentException("itemComponent may not be null");
            }
            this.itemComponent = itemComponent;
        }

//        public final int getLayoutPosition() {
//            return mPreLayoutPosition == NO_POSITION ? mPosition : mPreLayoutPosition;
//        }
    }
}
